Read in SF trees data

sf_trees<- read.csv(here("data", "sf_trees", "sf_trees.csv"))

Basic wrangling reminders

refresh skills for data wrangling and summary statistics using functions in the dplyer package

find top 5 highest obsv of trees by legal_status, do wrangling, make a graph using: count

top_five_status <- sf_trees %>% 
  count(legal_status) %>% #count does summarize, n function, and group by in one fucntion
  drop_na(legal_status) %>%  # for any variable, just need to specify can be multiple
  rename(tree_count = n) %>%  #NEW NAME GOES ON THE LEFT
  relocate(tree_count) %>%  #single column will move it to the front; must use new name bc rename came first
  slice_max(tree_count, n = 5) #identify rows with high values and only keeps top ones, there is also a min

Make a graph by top five observations by legal obsv

ggplot(top_five_status, aes(x = fct_reorder(legal_status, tree_count), y = tree_count))+ #change to factor associated with tree count associated with that
  geom_col()+
  labs(
    x = "Legal Status",
    y = "Tree Count"
  )+
  coord_flip()+
  theme_minimal()

More data wrangling

only want to keep obsv (rows) blackwood acacia trees

blackwood_acacia <-sf_trees %>% 
  filter(str_detect(species, "Blackwood Acacia")) %>%  #keep observations wehre a certain string is detected ANYWHERE within that variable
  select(legal_status, date, latitude, longitude)

ggplot(blackwood_acacia, aes(x = longitude, y = latitude))+ 
  geom_point()
## Warning: Removed 27 rows containing missing values (geom_point).

Tidy r separate and unite functions

usefull for combining and separating columns

common name and scientific name, split them using the :: in the dataset!

sf_trees_sep <- sf_trees %>% 
  separate(species, into = c("spp_scientific", "spp_common"), sep = "::")

example of tidyr unite

combine tree and legal status into same column. probaby wouldnt do this

sf_trees_unite <- sf_trees %>% 
  unite("id_status", tree_id:legal_status, sep = "_cool_")

Make some actual maps of blackwood acacia trees in SF

use st_as_sf() to convert the lat and long to spatial coordinates set a coord ref system

blackwood_acacia_sp <- blackwood_acacia %>% 
  drop_na(latitude, longitude) %>% 
  st_as_sf(coords = c("longitude", "latitude"))

st_crs(blackwood_acacia_sp)= 4326

ggplot(blackwood_acacia_sp)+
  geom_sf(color = "darkgreen")

read in sf roads shp file

sf_map <-read_sf(here("data", "sf_map", "tl_2017_06075_roads.shp")) #read spatial data so use sf

st_transform(sf_map, 4326)
## Simple feature collection with 4087 features and 4 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: -122.5136 ymin: 37.70813 xmax: -122.3496 ymax: 37.83213
## geographic CRS: WGS 84
## # A tibble: 4,087 x 5
##    LINEARID   FULLNAME     RTTYP MTFCC                                  geometry
##  * <chr>      <chr>        <chr> <chr>                          <LINESTRING [°]>
##  1 110498938… Hwy 101 S O… M     S1400 (-122.4041 37.74842, -122.404 37.7483, -…
##  2 110498937… Hwy 101 N o… M     S1400 (-122.4744 37.80691, -122.4746 37.80684,…
##  3 110366022… Ludlow Aly … M     S1780 (-122.4596 37.73853, -122.4596 37.73845,…
##  4 110608181… Mission Bay… M     S1400 (-122.3946 37.77082, -122.3929 37.77092,…
##  5 110366689… 25th Ave N   M     S1400 (-122.4858 37.78953, -122.4855 37.78935,…
##  6 110368970… Willard N    M     S1400 (-122.457 37.77817, -122.457 37.77812, -…
##  7 110368970… 25th Ave N   M     S1400 (-122.4858 37.78953, -122.4858 37.78952,…
##  8 110498933… Avenue N     M     S1400 (-122.3643 37.81947, -122.3638 37.82064,…
##  9 110368970… 25th Ave N   M     S1400  (-122.4854 37.78983, -122.4858 37.78953)
## 10 110367749… Mission Bay… M     S1400 (-122.3865 37.77086, -122.3878 37.77076,…
## # … with 4,077 more rows
ggplot(sf_map)+
  geom_sf()

use st_transform(sf map) to get the crs format!! dont want to mix crs

combine blackwood acacia tree obsv and sf roads map:

ggplot()+ #but nothing within it bc want to specify the data for each geom within the sf
  geom_sf(data = sf_map, size = 0.1, color = "darkgray")+
  geom_sf(data = blackwood_acacia_sp, color = "red", size = 0.5)+
  theme_void()

now to create an interactive map:

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(blackwood_acacia_sp) + # base layer
  tm_dots()